package com.xdf.xzymanagementsystem.service.impl;

import com.xdf.xzymanagementsystem.dao.CallProcedureMapper;
import com.xdf.xzymanagementsystem.entity.BillType;
import com.xdf.xzymanagementsystem.entity.DeliveryList;
import com.xdf.xzymanagementsystem.service.CallProcedureService;
import com.xdf.xzymanagementsystem.service.SubSystemClient;
import com.xdf.xzymanagementsystem.service.base.BaseService;
import com.xdf.xzymanagementsystem.util.DatetimeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hpsf.Decimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: xzy-management-system
 * @description:
 * @author: 张柯
 * @create: 2021-01-07 16:41
 **/
@Service
public class CallProcedureServiceImpl extends BaseService implements CallProcedureService {
    private CallProcedureMapper mapper;
    @Autowired
    private SubSystemClient subSystemClientService;

    @Autowired
    public CallProcedureServiceImpl(CallProcedureMapper mapper) {
        this.mapper = mapper;
    }

    /**
     * 销售订单单据编号生成
     *
     * @return
     * @throws Exception
     */
    @Override
    public String salesOrderCodeGen(String prefix) throws Exception {
        return mapper.salesOrderCodeGen(prefix);
    }

    /**
     * 销售计划
     *
     * @return
     * @throws Exception
     */
    @Override
    public String salesPlanCodeGen(String prefix) throws Exception {
        return mapper.salesPlanCodeGen(prefix);
    }

    /**
     * 发货计划生成
     *
     * @param prefix
     * @return
     * @throws Exception
     */
    @Override
    public String deliveryCodeGen(String prefix) throws Exception {
        return mapper.deliveryCodeGen(prefix);
    }

    /**
     * 成品发货单编号生成
     *
     * @param prefix
     * @return
     */
    @Override
    public String deliveryBillCodeGen(String prefix) {
        return mapper.deliveryBillCodeGen(prefix);
    }

    /**
     * 成品入库单编号生成
     *
     * @param prefix
     * @return
     */
    @Override
    public String productCodeGen(String prefix) {
        return mapper.productCodeGen(prefix);
    }

    /**
     * 销售订单序时表
     *
     * @param start 起始日期
     * @param end   终止日期
     * @return
     */
    @Override
    public List<Map<String, Object>> getSalesOrderReport(Date start, Date end, Boolean isFinished,Integer audit) {
        if (start == null || end == null) return null;
        List<Map<String, Object>> list = Optional.ofNullable(mapper.getSalesOrderReport(start, end,audit)).orElse(new ArrayList<>());
        //list = subSystemClientService.filterSubSystemUser(list);
        list = subSystemClientService.filter8SubSystemUserContact(list,"get","client_id");
        if (isFinished == null) return list;
        return list.stream().filter(item -> {
            Object i = item.get("is_finished");
            return i == null || ((int) i == 1) == isFinished;
        }).collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> getSalesOrderReportCustomer(Date start, Date end,Integer audit) {
        if (start == null || end == null) return null;
        List<Map<String, Object>> list = Optional.ofNullable(mapper.getSalesOrderReportCustomer(start, end,audit)).orElse(new ArrayList<>());
        return list;
    }

    /**
     * 销售计划序时表
     *
     * @param start 起始日期
     * @param end   终止日期
     * @return
     */
    @Override
    public List<Map<String, Object>> getSalesPlanReport(Date start, Date end) {
        if (start == null || end == null) return null;
        return mapper.getSalesPlanReport(start, end);
    }

    /**
     * 获取生产计划兼日报
     *
     * @param start
     * @param end
     * @return
     */
    @Override
    public List<Map<String, Object>> getPlanReport(Date start, Date end, Integer isFinished) {
        if (start == null || end == null) return null;
        List<Map<String, Object>> list = Optional.ofNullable(mapper.getPlanReport(start, end))
                .orElse(new ArrayList<>());
        if (isFinished == null) return list;
        String key = "flag_finished";
        return list.stream().filter(item -> item.containsKey(key)
                && isFinished == Integer.parseInt(String.valueOf(item.get(key) == null ? 0 : item.get(key))))
                .collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> getExecPlanReport(Date start, Date end, Integer isFinished) {
        if (start == null || end == null) return null;
        List<Map<String, Object>> list = Optional.ofNullable(mapper.getExecPlanReport(start, end))
                .orElse(new ArrayList<>());
        if (isFinished == null) return list;
        String key = "flag_finished";
        return list.stream().filter(item -> item.containsKey(key)
                        && isFinished == Integer.parseInt(String.valueOf(item.get(key) == null ? 0 : item.get(key))))
                .collect(Collectors.toList());
    }

    /**
     * 采购订单
     *
     * @param start
     * @param end
     * @return
     */
    @Override
    public List<Map<String, Object>> getPurchaseOrderReport(Date start, Date end) {
        if (start == null || end == null) return null;
        return mapper.getPurchaseOrderReport(start, end);
    }

    /**
     * 材料入库序时表
     *
     * @param start 起始日期
     * @param end   终止日期
     * @return
     */
    @Override
    public List<Map<String, Object>> getMaterialOrderBillReport(Date start, Date end) {
        return mapper.getMaterialOrderBillReport(start, end);
    }

    /**
     * 材料库存
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getMaterialStoreReport() {
        return mapper.getMaterialStoreReport();
    }

    @Override
    public List<Map<String, Object>> getMaterialStoreReport8Store(String storeSid){
        return mapper.getMaterialStoreReport8Store(storeSid);
    }

    /**
     * 材料收发存汇总表
     *
     * @param start   起始日期
     * @param end     终止日期
     * @param storeId 仓库编号
     * @return
     */
    @Override
    public List<Map<String, Object>> getMeterialTotalReport(Date start, Date end, String storeId) {
        return mapper.getMeterialTotalReport(start, end, storeId);
    }

    /**
     * 发货计划序时表
     *
     * @param start 起始日期
     * @param end   终止日期
     * @param mode  模式
     * @return
     */
    @Override
    public List<Map<String, Object>> getDeliveryPlanReport(Date start, Date end, Integer mode) {
        if (mode == null) mode = 0;
        return mapper.getDeliveryPlanReport(start, end, mode);
    }

    public List<Map<String, Object>> getDeliveryPlanReport(Date start, Date end, Integer mode,Integer isFinished) {
        if (mode == null) mode = 0;
        if (isFinished == null) isFinished = 0;
        List<Map<String, Object>> ret = mapper.getDeliveryPlanReport(start, end, mode);
        if(isFinished == 0) { // 只取未出库结束的
            ret = ret.stream().filter(item -> {
                BigDecimal amount = new BigDecimal(item.get("amount").toString());
                BigDecimal outputAmount = new BigDecimal(item.get("output_amount").toString());
                return amount.compareTo(outputAmount) == 1; // 计划数量大于已出库数量
            }).collect(Collectors.toList());
        }

        if(isFinished == 1) { // 只取出库结束的
            ret = ret.stream().filter(item -> {
                BigDecimal amount = new BigDecimal(item.get("amount").toString());
                BigDecimal outputAmount = new BigDecimal(item.get("output_amount").toString());
                int comparedResult = amount.compareTo(outputAmount);
                return comparedResult <= 0;// 计划数量小于等于已出库数量
            }).collect(Collectors.toList());
        }

        // isFinished !=0 && isFinished !=1
        // 则认为显示所有，则不需过滤
        return ret;
    }

    /**
     * 生产计划一览表
     *
     * @param start 起始日期
     * @param end   终止日期
     * @return
     */
    @Override
    public List<Map<String, Object>> getPlanReportSummary(Date start, Date end) {
        return mapper.getPlanReportSummary(start, end);
    }

    /**
     * 获取生产计划兼日报
     *
     * @param id 单据主键
     * @return
     */
    @Override
    public List<Map<String, Object>> getPlanReportRegisteDtl(Long id) {
        List<Map<String, Object>> result = Optional.ofNullable(mapper.getPlanReportRegisteDtl(id)).orElse(new ArrayList<>());
//        if (hasRole(RoleConstant.WORKSHOP_ROLE)) {
//            return result.stream().filter(x -> x.get("flagFinished") == null || 0 == (int) x.get("flagFinished")).collect(Collectors.toList());
//        }
//        return result;

        // 2021年8月7日 14:23:11 注释上面的代码，不理解当时为什么只有车间这样显示
        // 实际办公室端在该页面上也要按照是否完成来分标签页显示
        return result.stream().filter(x -> x.get("flagFinished") == null || 0 == (int) x.get("flagFinished")).collect(Collectors.toList());
    }

    /**
     * 追加染色印花序时簿字段
     *
     * @param machineCode
     * @param isFinished
     * @return
     */
    @Override
    public List<Map<String, Object>> getPlanReportRegisteDtl2(String machineCode, boolean isFinished) {
        List<Map<String, Object>> ret = mapper.getPlanReportRegisteDtl2(machineCode, isFinished);
        if( isFinished == true ) // 已完工页面按照入库日期降序
            Collections.sort(ret, new Comparator<Map<String, Object>>(){
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    Date date1 = (Date) o1.get("inboundDate");
                    try {
                        date1 = Optional.ofNullable(date1).orElse(DatetimeUtils.parseDate("1900-01-01","yyyy-MM-dd"));
                    } catch (Exception e) {
                        throw new RuntimeException("生产计划登记页已完工数据排序时出现异常！");
                    }
                    Date date2= (Date) o2.get("inboundDate");
                    try {
                        date2 = Optional.ofNullable(date2).orElse(DatetimeUtils.parseDate("1900-01-01","yyyy-MM-dd"));
                    } catch (Exception e) {
                        throw new RuntimeException("生产计划登记页已完工数据排序时出现异常！");
                    }
                    return date2.compareTo(date1);
                }
            });
        else // 未完工页面按照发货计划日期升序显示 sendOutDate
            Collections.sort(ret, new Comparator<Map<String, Object>>(){
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    Date date1 = (Date) o1.get("sendOutDate");
                    try {
                        date1 = Optional.ofNullable(date1).orElse(DatetimeUtils.parseDate("2500-01-01","yyyy-MM-dd"));
                    } catch (Exception e) {
                        throw new RuntimeException("生产计划登记页未完工数据排序时出现异常！");
                    }
                    Date date2= (Date) o2.get("sendOutDate");
                    try {
                        date2 = Optional.ofNullable(date2).orElse(DatetimeUtils.parseDate("2500-01-01","yyyy-MM-dd"));
                    } catch (Exception e) {
                        throw new RuntimeException("生产计划登记页未完工数据排序时出现异常！");
                    }
                    return date1.compareTo(date2);
                }
            });

        return ret;
    }

    public List<Map<String, Object>> getExecPlanReportRegisteDtl2_2024年10月31日备份(String machineCode, boolean isFinished) {
        List<Map<String, Object>> ret = mapper.getExecPlanReportRegisteDtl2(machineCode, isFinished);
        if( isFinished == true ) // 已完工页面按照入库日期降序
            Collections.sort(ret, new Comparator<Map<String, Object>>(){
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    Date date1 = (Date) o1.get("inboundDate");
                    try {
                        date1 = Optional.ofNullable(date1).orElse(DatetimeUtils.parseDate("1900-01-01","yyyy-MM-dd"));
                    } catch (Exception e) {
                        throw new RuntimeException("执行计划登记页已完工数据排序时出现异常！");
                    }
                    Date date2= (Date) o2.get("inboundDate");
                    try {
                        date2 = Optional.ofNullable(date2).orElse(DatetimeUtils.parseDate("1900-01-01","yyyy-MM-dd"));
                    } catch (Exception e) {
                        throw new RuntimeException("执行计划登记页已完工数据排序时出现异常！");
                    }
                    return date2.compareTo(date1);
                }
            });
        //else // 未完工页面按照发货计划日期升序显示 sendOutDate
//            Collections.sort(ret, new Comparator<Map<String, Object>>(){
//                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
//                    Date date1 = (Date) o1.get("sendOutDate");
//                    try {
//                        date1 = Optional.ofNullable(date1).orElse(DatetimeUtils.parseDate("2500-01-01","yyyy-MM-dd"));
//                    } catch (Exception e) {
//                        throw new RuntimeException("执行计划登记页未完工数据排序时出现异常！");
//                    }
//                    Date date2= (Date) o2.get("sendOutDate");
//                    try {
//                        date2 = Optional.ofNullable(date2).orElse(DatetimeUtils.parseDate("2500-01-01","yyyy-MM-dd"));
//                    } catch (Exception e) {
//                        throw new RuntimeException("执行计划登记页未完工数据排序时出现异常！");
//                    }
//                    return date1.compareTo(date2);
//                }
//            });
        // 2023年2月13日 执行计划保存时已经排好序，所以“未完工”标签页中不进行排序
        // 即按照单据保存时的顺序显示

        return ret;
    }

    /**
     * 获取生产计划兼日报已完工
     *
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> getFinishedPlanReportRegisteDtl(Long id) {
        List<Map<String, Object>> result = Optional.ofNullable(mapper.getPlanReportRegisteDtl(id)).orElse(new ArrayList<>());
//        System.out.println("获取到的生产计划的所有明细数据是：" + result.toString());
//        System.out.println("总共" + result.size() + "条数据");
        List<Map<String, Object>> finishedResult = result.stream().filter(x -> x.get("flagFinished") != null && 1 == (int) x.get("flagFinished")).collect(Collectors.toList());
//        System.out.println("所有已完工的数据是：" + finishedResult.toString());
//        System.out.println("完工数据共" + finishedResult.size() + "条");
        return finishedResult;
    }

    /**
     * 材料列表
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getMaterialList() {
        return mapper.getMaterialList();
    }

    /**
     * 成品入库存储过程
     *
     * @param start
     * @param end
     * @return
     */
    @Override
    public List<Map<String, Object>> getProductOrderReport(Date start, Date end) {
        return mapper.getProductOrderReport(start, end);
    }

    @Override
    public List<Map<String, Object>> getProductOrderStoragePDMult() {
        List<Map<String, Object>> ret = mapper.getProductOrderStoragePDMult();

        // 正常用户则只显示大于0的产品
        // 相反，如果是客户子系统的账号则显示所有产品 - 包含0
        Boolean isSubsystem = subSystemClientService.isSubUser();
        if(isSubsystem == false) ret = ret.stream().filter(item -> {
            BigDecimal amount = (BigDecimal) Optional.ofNullable(item.get("amount")).orElse(0);
            int comp = amount.compareTo(BigDecimal.valueOf(0));
            return comp > 0 ? true : false;
        }).collect(Collectors.toList());

        List<Map<String, Object>> ret1 = subSystemClientService.filter8SubSystemUserContact(ret,"get","client_id");
        return ret1;
    }

    /**
     * 发货单序时表
     *
     * @param start 起始日期
     * @param end   终止日期
     * @return
     */
    @Override
    public List<Map<String, Object>> getDeliveryBillReport(Date start, Date end) {
        return mapper.getDeliveryBillReport(start, end);
    }

    /**
     * 获取未完工发货计划存储过程
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getUnfinishedDeliveryPlan() {
        return mapper.getUnfinishedDeliveryPlan();
    }

    /**
     * 无发货计划订单序时表
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getNondeliveryPlanOrderReport() {
        return mapper.getNondeliveryPlanOrderReport();
    }

    /**
     * 成品出库单序时表
     *
     * @param start
     * @param end
     * @return
     */
    @Override
    public List<Map<String, Object>> getOutboundProductOrderReport(Date start, Date end) {
        return mapper.getOutboundProductOrderReport(start, end);
    }

    /**
     * 出库一览
     * @param start
     * @param end
     * @param contactCompanyId
     * @return
     */
    @Override
    public List<Map<String, Object>> getDeliveryList(Date start, Date end, Integer contactCompanyId) {
        // 查询数据前先更新客户排序序号
        Integer ret = mapper.updateClientSeq();
        List<Map<String, Object>> list = Optional.ofNullable(mapper.getDeliveryList(start, end)).orElse(new ArrayList<>());
        if (contactCompanyId == null) return list;

//        List<Map<String, Object>> ret = new ArrayList<>();
//        if( contactCompanyId > 0 ){
//            for( Map<String, Object> item:list ){
//                if( contactCompanyId.equals(item.get("contact_company_id")))
//                    ret.add(item);
//            }
//            return ret;
//        }
//        return list;
        return list.stream()
                .filter(x -> x.get("contact_company_id") != null && String.valueOf(contactCompanyId).equals(String.valueOf(x.get("contact_company_id"))))
                .collect(Collectors.toList());
    }

    /**
     * 未完成发货计划
     *
     * @param contactCompanyId
     * @return
     */
    @Override
    public List<Map<String, Object>> getUnfinishedDeliveryPlanDataSource(Integer contactCompanyId) {
        if (contactCompanyId == null) return null;
        return mapper.getUnfinishedDeliveryPlanDataSource(contactCompanyId);
    }

    /**
     * 发货对账单序时表
     *
     * @param start 起始日期
     * @param end   终止日期
     * @return
     */
    @Override
    public List<Map<String, Object>> getStatementOfAccountReport(Date start, Date end) {
        return mapper.getStatementOfAccountReport(start, end);
    }

    /**
     * 获取当前材料库存
     *
     * @param storageId
     * @param productCoded
     * @return
     */
    @Override
    public BigDecimal getMaterialStorage(String storageId, String productCoded) {
        return mapper.getMaterialStorage(storageId, productCoded);
    }

    /**
     * 查询当前未完工的存货档案
     *
     * @param productCoded
     * @return
     */
    @Override
    public List<Map<String, Object>> getPurchaseOrderList(String productCoded) {
        if (StringUtils.isEmpty(productCoded)) {
            productCoded = "";
        }
        return mapper.getPurchaseOrderList(productCoded);
    }

    @Override
    public List<Long> getDeliveryList2Price() {
        return mapper.getDeliveryList2Price();
    }

    @Override
    public List<Long> getDeliveryListNoPrice() {
        return mapper.getDeliveryListNoPrice();
    }

    /**
     * 模糊查询已开过订单的存货编码
     *
     * @param filter
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> getFuzzylookupSalesorderProductFile(String filter) throws Exception {
        if (StringUtils.isEmpty(filter)) return null;
        return mapper.getFuzzylookupSalesorderProductFile(filter);
    }

    @Override
    public List<DeliveryList> getDeliveryListNoPrice4Fix() {
        return mapper.getDeliveryListNoPrice4Fix();
    }

    @Override
    public List<DeliveryList> getDeliveryList2Price4Fix() {
        return mapper.getDeliveryList2Price4Fix();
    }

    @Override
    public List<Map<String, Object>> getAllDeliveryListIds() {
        return mapper.getAllDeliveryListIds();
    }

    /**
     * 2021年8月24日 23:19:08 chanchaw
     * 更新 出货一览表 中所有明细数据的单价
     * @return
     */
    @Override
    public Integer updateDeliveryAllPrice() {
        List<Map<String,Object>> list = getAllDeliveryListIds();
        if( list.size()<=0 ) throw new RuntimeException("出货一览表中没有数据！");
        for (Map<String, Object> item : list) {
            String type = Optional.ofNullable((String) item.get("type")).orElse("");
            if( type.length()<=0 ) continue;
            Long id = (Long) Optional.ofNullable(item.get("id")).orElse(0);
            if( id <=0 ) continue;

            switch(type){
                case "销售订单":
                    updateSalesOrderDtlPrice(id);
                    break;
                case "发货计划":
                    updateDeliveryPlanPrice(id);
                    break;
                case "成品出库":
                    updateProductOrderPrice(id);
                    break;
                case "对账附加":
                    updateStatementAccount(id);
                    break;
                default:
                    System.out.println(item.toString());
                    //throw new RuntimeException("更新出货一览表单价金额时出现异常");
            }

        }
        return 1;
    }

    /**
     * 更新指定主键的销售订单明细数据的单价和金额
     * @param id
     * @return
     */
    @Override
    public Integer updateSalesOrderDtlPrice(Long id){
        id = Optional.ofNullable(id).orElse(Long.valueOf(0));
        if( id <=0 ) return 0;
        return mapper.updateSalesPrice(id);
    }

    /**
     * 2021年8月25日 21:22:32 chanchaw
     * 传入发货计划明细数据主键修改其单价和金额 - 以存货档案为准
     * @param id 发货计划明细数据主键
     * @return 返回影响的行数
     */
    @Override
    public Integer updateDeliveryPlanPrice(Long id){
        id = Optional.ofNullable(id).orElse(Long.valueOf(0));
        if( id <=0 ) return 0;
        return mapper.updateDeliveryPlanPrice(id);
    }

    /**
     * 传入成品出库明细表主键，修改对应的发货指示明细数据逇单价和金额
     * @param id 成品出库明细表主键
     * @return 返回影响的行数
     */
    @Override
    public Integer updateProductOrderPrice(Long id){
        id = Optional.ofNullable(id).orElse(Long.valueOf(0));
        if( id <=0 ) return 0;
        return mapper.updateProductOrderPrice(id);
    }

    @Override
    public Integer updateStatementAccount(Long id) {
        return mapper.updateStatementAccount(id);
    }

    @Override
    public List<Map<String, Object>> getUserOnlineStatus() {
        return mapper.getUserOnlineStatus();
    }

    @Override
    public void delDuplicateQualifier() {
        mapper.delDuplicateQualifier();
    }

    @Override
    public List<Map<String,Object>> rptQuotation(Date sdate, Date edate, Integer state){
        return mapper.rptQuotation(sdate, edate, state);
    }

    @Override
    public List<BillType> getMaterialBillType(){
        return mapper.getMaterialBillType();
    }

    @Override
    public List<Map<String, Object>> getProductCraft8ProductCategories(List<Integer> categoryIdList){
        return mapper.getProductCraft8ProductCategories(categoryIdList);
    }
    @Override
    public List<Map<String, Object>> getExecPlanReportRegisteDtl2(String machineCode, boolean isFinished) {
        return getExecPlanReportRegisteDtl2Partial(machineCode,isFinished);
    }
    /**
     * 2024年10月31日 15:25:06
     * 将原本的 getExecPlanReportRegisteDtl2 调用的存储过程拆分为3个子逻辑的存储过程
     * 后台需要分别调用3个子存储过程后拼接数据，注意要排序：ORDER BY execPlan,parentId,serialNo
     * @param machineCode
     * @param isFinished
     * @return
     */
    @Override
    public List<Map<String, Object>> getExecPlanReportRegisteDtl2Partial(String machineCode, boolean isFinished){
        List<Map<String, Object>> ret01 = mapper.getExecPlanReportRegisteDtl2Partial01(machineCode, isFinished);
        List<Map<String, Object>> ret02 = mapper.getExecPlanReportRegisteDtl2Partial02(machineCode, isFinished);
        List<Map<String, Object>> ret03 = mapper.getExecPlanReportRegisteDtl2Partial03(machineCode, isFinished);
        List<Map<String, Object>> ret = new ArrayList<>();
        ret.addAll(ret01);ret.addAll(ret02);ret.addAll(ret03);

        // 依次使用3个属性排序：execPlan,parentId,serialNo
        Collections.sort(ret, new Comparator<Map<String, Object>>(){
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Long execPlan1 = (Long) o1.get("execPlan");
                Long execPlan2 = (Long) o2.get("execPlan");
                Long parentId1 = (Long) o1.get("parentId");
                Long parentId2 = (Long) o2.get("parentId");
                Integer serialNo1 = (Integer) o1.get("serialNo");
                Integer serialNo2 = (Integer) o2.get("serialNo");

                // 将多个要素比较的结果拼接为字符串后进行排序
                int i1 = execPlan1.compareTo(execPlan2) * 100;
                int i2 = parentId1.compareTo(parentId2) * 10;
                int i3 = serialNo1.compareTo(serialNo2);
                return i1+i2+i3;
            }
        });

        if( isFinished == true ) { // 已完工页面按照入库日期降序
            Collections.sort(ret, new Comparator<Map<String, Object>>() {
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    Date date1 = (Date) o1.get("inboundDate");
                    try {
                        date1 = Optional.ofNullable(date1).orElse(DatetimeUtils.parseDate("1900-01-01", "yyyy-MM-dd"));
                    } catch (Exception e) {
                        throw new RuntimeException("执行计划登记页已完工数据进行排序时对入库日期进行格式化出现异常！");
                    }
                    Date date2 = (Date) o2.get("inboundDate");
                    try {
                        date2 = Optional.ofNullable(date2).orElse(DatetimeUtils.parseDate("1900-01-01", "yyyy-MM-dd"));
                    } catch (Exception e) {
                        throw new RuntimeException("执行计划登记页已完工数据进行排序时对入库日期进行格式化出现异常！");
                    }
                    return date2.compareTo(date1);
                }
            });
        }
        return ret;
    }
}